#include "LocalScene.h"
#include "Utils/LogUtil.h"
#include "LocalEntity.h"
#include "Utils/StringUtil.h"
#include "Render/RenderState.h"
#include "Render/Imgui/imgui.h"
#include "Math/Random.h"
#include "Game/GameState.h"
#include "Game/Component/SceneModel.h"
#include "DeferredProcess.h"


void LocalScene::OnInit()
{
	//mCamera.LookAt(glm::vec3(0.f, 35.f, 10.f), glm::vec3(0.f, 0.f, -25.f));
	mCamera.LookAt(glm::vec3(0.f, 70.f, 0.f), glm::vec3(0.f, 0.f, -0.1f));
	mCamera.UpdateViewMatrix();

	mLightProj = mCamera.GetProj();
	// mLightProj = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, 0.1f, 512.0f);;
	mLightView = mCamera.GetView();
	mLightPosition = mCamera.GetPosition();
	mLightDirection = glm::vec3(mLightView * mCamera.GetLook4f());



	mCamera.SetSpeed(16.f);
	// mCamera.SetPosition(glm::vec3(5.f, 35.f, 0.f));
	mCamera.LookAt(glm::vec3(5.f, 35.f, 0.f), glm::vec3(-30, 0, 0));
	mCamera.UpdateViewMatrix();



	mCoordinateProcess = GetProcess<CoordinateProcess>("CoordinateProcess");
	mDeferredProcess = GetProcess<DeferredProcess>("DeferredProcess");

	LocalEntity* sceneEntity = CreateEntity<LocalEntity>("scene");
	sceneEntity->AddComponent<SceneModel>("SponzaFull.obj");
	sceneEntity->GetTransform().SetLocalScale(glm::vec3(0.05f));
	sceneEntity->GetTransform().UpdateMatrices();

	mIsRotate = true;
	mLightTransformOperation = ImGuizmo::ROTATE;
	mLightPosition = glm::vec3(35.f, 35.f, 0.f);
	mLightColor = glm::vec3(1.0f);
	mLightIntensity = 10.f;

	/*glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(-36.0f), glm::vec3(1.0f, 0.0f, 0.0f))
		* glm::rotate(glm::mat4(1.0f), glm::radians(90.0f), glm::vec3(0.0f, 1.0f, 0.0f))
		* glm::rotate(glm::mat4(1.0f), glm::radians(-180.0f), glm::vec3(0.0f, 0.0f, 1.0f));*/
	/*glm::mat4 R = glm::rotate(glm::mat4(1.0f), glm::radians(0.f), glm::vec3(1.0f, 0.0f, 0.0f))
		* glm::rotate(glm::mat4(1.0f), glm::radians(-62.f), glm::vec3(0.0f, 1.0f, 0.0f))
		* glm::rotate(glm::mat4(1.0f), glm::radians(60.0f), glm::vec3(0.0f, 0.0f, 1.0f));*/
	//glm::mat4 R = glm::mat4(1.f);// glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
	//glm::mat4 T = glm::translate(glm::mat4(1.0f), mLightPosition);
	//mLightTransform = T * R;


	
	//mLightPosition = glm::vec3(35.f, 35.f, 0.f);
	//mLightDirection = glm::normalize(glm::vec3(0, 0, 0) - mLightPosition);

	//mLightProj = glm::perspective((float)Math::M_HALF_PI, 1.0f, 0.1f, 512.0f);
	//// mLightProj = glm::ortho(-20.0f, 20.0f, -20.0f, 20.0f, 0.1f, 512.0f);
	//
	//glm::vec3 lightRight = glm::normalize(glm::cross(mLightDirection, glm::vec3(0.f, 1.f, 0.f)));
	//glm::vec3 lightUp = glm::cross(lightRight, mLightDirection);

	//mLightView = glm::lookAt(mLightPosition, glm::vec3(0, 0, 0), glm::vec3(0.0f, -1.0f, 0.0f));
	
}

void LocalScene::OnTick(float deltaTime)
{
	 // mLightDirection = glm::normalize(glm::mat3(mLightTransform) * glm::vec3(0.0f, -1.0f, 0.0f));
	 // mLightPosition = glm::vec3(mLightTransform[3][0], mLightTransform[3][1], mLightTransform[3][2]);

	 // mLightProj = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, 0.1f, 10.0f);
	 // mLightProj = glm::perspective((float)Math::M_PI, 1.0f, 0.1f, 512.0f);
	 // mLightView = glm::lookAt(mLightPosition, mLightPosition + mLightDirection, glm::vec3(1, 0, 0));
}

void LocalScene::OnImgui()
{
	if (ImGui::BeginMainMenuBar())
	{
		ImGui::Text("Fps : %d ", GRenderState->GetFps());

		ImGui::EndMainMenuBar();
	}


	ImGui::ColorEdit3("Color", &mLightColor.x);
	ImGui::InputFloat("Intensity", &mLightIntensity);

	ImGui::RadioButton("Full", &mDeferredProcess->mDebugIndex, 0);
	ImGui::RadioButton("Indirect", &mDeferredProcess->mDebugIndex, 1);
	ImGui::RadioButton("Direct", &mDeferredProcess->mDebugIndex, 2);


	/*glm::vec3 cameraPosition = mCamera.GetPosition();
	glm::vec3 cameraRotation = mCamera.GetRotation();

	ImGui::InputFloat3("Rotation", &cameraRotation.x);
	ImGui::InputFloat3("Position", &cameraPosition.x);*/



	/*ImGui::Checkbox("Light Rotate", &mIsRotate);
	mLightTransformOperation = mIsRotate ? ImGuizmo::ROTATE : ImGuizmo::TRANSLATE;

	ImGui::ColorEdit3("Color", &mLightColor.x);
	ImGui::InputFloat("Intensity", &mLightIntensity);

	glm::vec3 position;
	glm::vec3 rotation;
	glm::vec3 scale;

	ImGuizmo::BeginFrame();
	ImGuizmo::SetOrthographic(false);

	ImGuizmo::DecomposeMatrixToComponents(&mLightTransform[0][0], &position.x, &rotation.x, &scale.x);

	ImGui::InputFloat3("Rotation", &rotation.x);
	ImGui::InputFloat3("Position", &position.x);

	ImGuizmo::RecomposeMatrixFromComponents(&position.x, &rotation.x, &scale.x, &mLightTransform[0][0]);

	ImGuizmo::SetRect(0, 0, GRenderState->GetWidth(), GRenderState->GetHeight());
	ImGuizmo::Manipulate(&mCamera.mZmoView[0][0], &mCamera.mProj[0][0], mLightTransformOperation, ImGuizmo::LOCAL, &mLightTransform[0][0], NULL, NULL);*/
}

void LocalScene::OnExit()
{

}